package org.jxc.web.action;

import java.util.List;

import org.jxc.domain.Product;
import org.jxc.domain.ProductType;
import org.jxc.service.ProductService;
import org.jxc.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class ProductAction extends GenericAction<Product> {

	private static final long serialVersionUID = -4867867482994497216L;

	@Autowired
	private ProductService productService;
	@Autowired
	private ProductTypeService productTypeService;
	private List<ProductType> productTypeList;
	private Product product;
	private String productId;
	private String productTypeId;
	private boolean unique;
	private String key;
	private String value;

	/**
	 * 类别树
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listTree() throws Exception {
		productTypeList = productTypeService.findAllProductType();
		return SUCCESS;
	}

	public String listPagination() throws Exception {
		page = productService.listPagination(page, productTypeId);
		return SUCCESS;
	}

	public String chooseTree() throws Exception {
		productTypeList = productTypeService.findAllProductType();
		return SUCCESS;
	}

	public String choosePagination() throws Exception {
		page = productService.listPagination(page, productTypeId);
		return SUCCESS;
	}

	/**
	 * 预新建
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toSave() throws Exception {
		productTypeList = productTypeService.findAllProductType();
		return SUCCESS;
	}

	/**
	 * 新建
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		productService.saveProduct(product);
		return SUCCESS;
	}

	/**
	 * 预修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toUpdate() throws Exception {
		product = productService.findById(productId);
		productTypeList = productTypeService.findAllProductType();
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		productService.updateProduct(product);
		return SUCCESS;
	}

	/**
	 * 查看
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toView() throws Exception {
		product = productService.findById(productId);
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		for (String str : idList) {
			try {
				productService.deleteProduct(str);
			} catch (Exception e) {
				continue;
			}
		}
		return SUCCESS;
	}

	public String uniqueValidate() throws Exception {
		unique = productService.uniqueValidate(key, value);
		return SUCCESS;
	}

	public List<ProductType> getProductTypeList() {
		return productTypeList;
	}

	public void setProductTypeList(List<ProductType> productTypeList) {
		this.productTypeList = productTypeList;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductTypeId() {
		return productTypeId;
	}

	public void setProductTypeId(String productTypeId) {
		this.productTypeId = productTypeId;
	}

	public boolean isUnique() {
		return unique;
	}

	public void setUnique(boolean unique) {
		this.unique = unique;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}